package ventureco.communication;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;

public class Broadcast extends Thread{
	
	public static int broadcastPort = 6666;
	private static ServerSocket serverSock = null;
	public static int i = 0;
	
	public static HashMap<Integer, Integer> ipAddresses = new HashMap<Integer, Integer>();
	
	public Broadcast() {
		super();
	}
	
	public void run() {

		 try {
			serverSock = new ServerSocket(broadcastPort);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		 
		 Socket sock = null;
			InputStream sockInput = null;
//			OutputStream sockOutput = null;
			while (true) {
			    try {
				// This method call, accept(), blocks and waits
				// (forever if necessary) until some other program
				// opens a socket connection to our server. 
				sock = serverSock.accept();
				System.err.println("Have accepted new socket.");

				// From this point on, no new socket connections can
				// be made to our server until we call accept() again.

				sockInput = sock.getInputStream();
//				sockOutput = sock.getOutputStream();
			    }
			    catch (IOException e){
				e.printStackTrace(System.err);
			    }
			    
//			    while(true) {
				    byte[] buf=new byte[1024];
				    int bytes_read = 0;
				    try {
				    	
					bytes_read = sockInput.read(buf, 0, buf.length);

					// If the socket is closed, sockInput.read() will return -1.
					if(bytes_read < 0) {
					    System.err.println("Tried to read from socket, read() returned < 0,  Closing socket.");
					    return;
					}
					
					byte[] purebuf = new byte[buf.length - 1];
					System.arraycopy(buf, 1, purebuf, 0, buf.length - 1);
					
					byte lasttwo = buf[0];
//					System.out.println(lasttwo);
					int tempport = ((int) lasttwo) + 5500;
					//String readMessage = new String(buf, 0, bytes_read);
//					System.out.println(tempport + 5500);
					
//					System.out.println(tempport);
					ipAddresses.put(i, tempport);
					i++;
					
//					for (int i = 0; i < ipAddresses.size(); i++) {
//						System.out.println(ipAddresses.get(i));
//						
//					}
//					System.out.println(tempport);
					for(int i=5555; i<tempport+1; i++) {
						//send addresslist to each agent
						ByteArrayOutputStream baos = new ByteArrayOutputStream();
						ObjectOutputStream objout;
						try {
							objout = new ObjectOutputStream(baos);
							objout.writeObject(ipAddresses);
						} catch (IOException e) {
							
							e.printStackTrace();
						}
						byte[] dataToSend = baos.toByteArray();
						
						byte[] pureDataToSend = new byte[dataToSend.length + 1];
						pureDataToSend[0] = 0;
						System.arraycopy(dataToSend, 0, pureDataToSend, 1, dataToSend.length);
					//	sockOutput.write(dataToSend, 0, bytes_read);
//						
						
						ThreadClient threadclient = new ThreadClient("localhost", i, pureDataToSend,0);
						threadclient.start();
//						
						sleep(200);
					}
					
//					System.err.println("Received "+bytes_read
//							   +" bytes, sending them back, data="
//							   +(new String(buf, 0, bytes_read)));
//				
					//sockOutput.write(buf, 0, bytes_read);
					//		sockOutput.flush();
				    }
				    catch (Exception e){
					System.err.println("Exception reading from/writing to socket, e="+e);
					e.printStackTrace(System.err);
					return;
				    }
			    

			}
	}

	
}
